package com.collectplus.express.order;

import java.util.ArrayList;
import java.util.HashSet;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.collectplus.express.R;
import com.collectplus.express.evaluate.EvaluateActivity;
import com.collectplus.express.model.OrderEvaluateBean;

import droid.frame.activity.base.BaseAdapterExt;
import droid.frame.utils.lang.Str;

/**
 * 快递员服务评价的按钮
 *
 * @author HuXinDan<br>
 *         2016-11-8下午3:16:42
 */
public class EvaluateItemAdapter extends BaseAdapterExt<OrderEvaluateBean> {

	private HashSet<Integer> checkedSet = new HashSet<Integer>();

	public EvaluateItemAdapter(ArrayList<OrderEvaluateBean> items, Activity context) {
		super(items, context);
	}

	@SuppressLint({ "NewApi", "ViewHolder" })
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder holder ;
		if(convertView == null|| convertView.getTag() == null){
			holder= new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.order_score_item, parent, false);
			holder.item = (TextView) convertView.findViewById(R.id.order_item);
			convertView.setTag(holder);
		}else {
			holder = (ViewHolder) convertView.getTag();
		}
		initItemView(position, holder);
		return convertView;
	}

	private void initItemView(int position, ViewHolder holder) {
		final OrderEvaluateBean item = getItem(position);
			holder.item.setText(item.getName());
		holder.item.setVisibility(View.VISIBLE);
		((ViewGroup) holder.item.getParent()).setVisibility(View.VISIBLE);
		holder.item.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				TextView textView = (TextView) v;
				if (v.getTag() == null) {// 当前未选中 --> 选中
					checkedSet.add(item.getId());
					textView.setBackgroundResource(R.drawable.evaluate_select_shape);
					textView.setTextColor(Color.parseColor("#F7622E"));
					textView.setTag("");// 已选中
				} else {// 当前未选中 --> 选中
					checkedSet.remove(item.getId());
					textView.setBackgroundResource(R.drawable.coupan_button_shape);
					textView.setTextColor(context.getResources().getColor(R.color.app_font_2));
					textView.setTag(null);
				}
				EvaluateActivity activity = (EvaluateActivity) context;
				activity.updateCheckedItems();
			}
		});
	}

	public void setupLayout(ViewGroup layout) {
		// LinearLayout layout = (LinearLayout) LayoutInflater.from(context).inflate(layoutId, parent, false);
		for (int position = 0; position < getCount(); position++) {
			int itemViewId = layout.getResources().getIdentifier("evaluate_item_" + (position + 1), "id", context.getPackageName());
			final ViewHolder holder = new ViewHolder();
			holder.item = (TextView) layout.findViewById(itemViewId);
			initItemView(position, holder);
		}
	}

	class ViewHolder {
		TextView item;
	}

	public HashSet<Integer> getCheckedSet() {
		return checkedSet;
	}
}
